﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace PengSW.Languages
{
    public static class LanguageHelper
    {
        public static Dictionary<string, string> Languages { get; } = new Dictionary<string, string>
        {
            {"中文", "pack://application:,,,/PengSW_Languages;component/zh-cn.xaml" },
            {"English", "pack://application:,,,/PengSW_Languages;component/en-us.xaml"},
        };

        public static void SetLanguage(string aLanguageKey)
        {
            foreach (var aResourceDictionary in (from r in Application.Current.Resources.MergedDictionaries where r.Source.OriginalString.Contains("/PengSW_Language;") select r).ToArray())
            {
                Application.Current.Resources.MergedDictionaries.Remove(aResourceDictionary);
            }

            string aLanguage = Languages[aLanguageKey];
            if (!string.IsNullOrWhiteSpace(aLanguage))
                Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri($"{aLanguage}", UriKind.Absolute) });
        }

        public static string GetStringResource(string aKey) => Application.Current.Resources[aKey] as string;
        public static string _T(string aKey, string aDefault = null) => GetStringResource(aKey) ?? aDefault;
    }
}
